var _oXslNumProc = null;
var _oXmlNumber = null;


var _aFullWidthSymbols = new Array(/\uFF41/g, /\uFF4D/g, /\uFF50/g, /\uFF21/g, /\uFF2D/g, /\uFF30/g, /\u2019/g, /\u3000/g, /\uFF1A/g, /\u3001/g, /\uFF0C/g, /\uFF0D/g, /\u30FC/g, /\u2015/g, /\u2010/g, /\uFF0E/g, /\u3002/g, /\uFF0F/g, /\uFF10/g, /\uFF11/g, /\uFF12/g, /\uFF13/g, /\uFF14/g, /\uFF15/g, /\uFF16/g, /\uFF17/g, /\uFF18/g, /\uFF19/g);
var _aHalfWidthSymbols = new Array("a"      , "m"      , "p"      , "A"      , "M"      , "P"      , "'"      , " "      , ":"      , ","      , ","      , "-"      , "-"      , "-"      , "-"      , "."      , "."      , "/"      , "0"      , "1"      , "2"      , "3"      , "4"      , "5"      , "6"      , "7"      , "8"      , "9");








function LocStringToInt(sValue)
{
try
{
return parseInt(RemoveFormatting(sValue), 10);
}
catch (e)
{
return NaN;
}
}

function LocStringToFloat(sValue)
{
try
{
return parseFloat(RemoveFormatting(sValue));
}
catch (e)
{
return NaN;
}
}


function AddFormatting(vValue, iAccuracy, bNoTrailingZeros)
{
try
{


var bSameFormattingSeparator = false ;
var sDecimalSymbol = USER_NUMBER_DECIMAL_SEPARATOR;
var sGroupSymbol = USER_NUMBER_GROUP_SEPARATOR;
var sNumberFormatMask = USER_NUMBER_FORMAT_MASK;
if ( USER_NUMBER_DECIMAL_SEPARATOR  == USER_NUMBER_GROUP_SEPARATOR )
{
bSameFormattingSeparator = true ;


sDecimalSymbol = "." ;


sGroupSymbol = "," ;


sNumberFormatMask = DEFAULT_USER_NUMBER_FORMAT_MASK ;
}

if (!_oXslNumProc || !_oXmlNumber)
{
var oXslDoc = CreateXmlDocument(true);
oXslDoc.load("/_controls/number/formatNumber.xsl");



var oNodes = oXslDoc.selectSingleNode("xsl:stylesheet/xsl:decimal-format").attributes ;
var count = 0 ;

while ( count < oNodes.length )
{
if ( oNodes[count].name == "decimal-separator")
{
oNodes[count].value = sDecimalSymbol ;
}
else
if ( oNodes[count].name == "grouping-separator")
{
oNodes[count].value = sGroupSymbol ;
}
count++;
}

var oXslt = CreateXslTemplate();
oXslt.stylesheet = oXslDoc;

_oXmlNumber = CreateXmlDocument(false);

_oXslNumProc = oXslt.createProcessor();
}

var sAccuracy = bNoTrailingZeros ? "##########" : "0000000000";


var sValue = new String("");
switch (typeof(vValue))
{

case "string":
sValue = vValue;

if (sValue.indexOf('e') > -1)
{
var nValue = new Number(sValue);
sValue = nValue.toFixed(10);
}
break;

case "number":

if (!IsNull(iAccuracy))
{
sValue = vValue.toFixed(iAccuracy);
}

else
{
sValue = vValue.toString();

if (sValue.indexOf('e') > -1)
{
sValue = vValue.toFixed(10);
}
}
break;
default:
throw new ArgumentException();
}


var iIndex = sValue.indexOf('.');
if(IsNull(iAccuracy))
{
iAccuracy = iIndex > -1 ? sValue.length - (iIndex + 1) : 0;
}

if (iIndex > -1)
{
sValue = sValue.substring(0, iIndex + iAccuracy + 1 );
}

var sPattern = sNumberFormatMask.replace(/\*/g, sAccuracy.substr(0, iAccuracy));

_oXslNumProc.addParameter("pattern", sPattern);

_oXmlNumber.loadXML("<number>" + sValue + "</number>");
_oXslNumProc.input = _oXmlNumber;

_oXslNumProc.transform();

var retval = _oXslNumProc.output;


if ( bSameFormattingSeparator )
{

retval = retval.replace(new RegExp("\\" + ",", "g"), USER_NUMBER_GROUP_SEPARATOR);
retval = retval.replace(new RegExp("\\" + ".", "g"), USER_NUMBER_DECIMAL_SEPARATOR);
}

return retval ;
}
catch  (e)
{
return "";
}
}

function FullWidthToHalfWidth(sValue)
{

if (typeof(sValue) == "string" ||
(!IsNull(sValue) && typeof(sValue) == "object" && typeof(sValue.substring) == "function"))
{

var i;
for (i = 0; i < _aFullWidthSymbols.length; i++)
{
sValue = sValue.replace(_aFullWidthSymbols[i], _aHalfWidthSymbols[i]);
}
}

return sValue;
}




function RemoveFormatting(vValue)
{
var sVal = new String(vValue);
var oFormatError = new Error(101, LOCID_ENTER_VALID_NUMBER);

if (null == sVal || "" == sVal) return sVal;


sVal = FullWidthToHalfWidth(sVal);


if ("." != USER_NUMBER_GROUP_SEPARATOR && "." != USER_NUMBER_DECIMAL_SEPARATOR && sVal.indexOf(".") != -1)
{
throw oFormatError;
}

sVal = sVal.replace(/\s*/g, "");
sVal = sVal.replace(/\xA0*/g, "");

sVal = sVal.replace(new RegExp("\\" + USER_NUMBER_GROUP_SEPARATOR, "g"), "");
sVal = sVal.replace(new RegExp("\\" + USER_NUMBER_DECIMAL_SEPARATOR, "g"), ".");

if ("" == sVal) return sVal;


var iIndex = sVal.length - 1;
if ('(' == sVal.charAt(0) && ')' == sVal.charAt(iIndex))
{
sVal = "-" + sVal.substring(1, iIndex);
}
else if ('-' == sVal.charAt(iIndex))
{
sVal = "-" + sVal.substring(0, iIndex);
}

if (null == sVal.match(/^-?((\d+\.?\d*)|(\.\d+))$/))
{
throw  oFormatError;
}

return sVal;
}
